﻿@using Microsoft.AspNet.Identity.EntityFramework;
@{
    ViewBag.Title = "Index";
    var perms = ViewBag.Permissions as List<Permission>;
}

<h2 style="color:red;">Phân Quyền</h2>
@foreach (IdentityRole r in ViewBag.Roles)
{
    <fieldset class="permissionad">
        <legend>@r.Name</legend>
        <ul>
            @foreach (WebAction wa in ViewBag.WebActions)
            {
                <li class="col-md-3">
                    <label>
                        @if (perms.Any(p => p.RoleId == r.Id && p.WebActionId == wa.Id && p.Allow == true))
                        {
                            <input checked type="checkbox" data-role="@r.Id" data-action="@wa.Id" />  @wa.Description
                        }
                        else
                        {
                            <input type="checkbox" data-role="@r.Id" data-action="@wa.Id" />  @wa.Description
                        }
                    </label>
                </li>
            }
        </ul>
    </fieldset>
}

@section scripts{
    <script>
        $(function () {
            $(".permissionad :checkbox").click(function () {
                var rid = $(this).attr("data-role");
                var aid = $(this).attr("data-action");

                $.ajax({
                    url: "/Admin/Permission/Update",
                    data: { RoleId: rid, ActionId: aid },
                    success: function (response) {
                        //alert(response);
                    }
                });
            });
        });
    </script>
}
